package com.esri.android.map.popup;

import com.esri.android.map.ags.ArcGISPopupInfo;
import com.esri.core.internal.util.g;
import com.esri.core.map.CodedValueDomain;
import com.esri.core.map.Domain;
import com.esri.core.map.FeatureTemplate;
import com.esri.core.map.FeatureType;
import com.esri.core.map.Field;
import com.esri.core.map.InheritedDomain;
import com.esri.core.map.popup.PopupFieldInfo;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* loaded from: classes.dex */
public class ArcGISValueFormat {
    private Popup c;
    private static final NumberFormat b = NumberFormat.getInstance(Locale.getDefault());
    static final DateFormat a = DateFormat.getDateInstance();

    public ArcGISValueFormat(Popup popup) {
        this.c = popup;
    }

    public String formatValue(Object obj, PopupFieldInfo popupFieldInfo) {
        return formatValue(this.c.mFeatureType, obj, popupFieldInfo);
    }

    public String formatValue(FeatureType featureType, Object obj, PopupFieldInfo popupFieldInfo) {
        return obj == null ? "" : popupFieldInfo == null ? a(featureType, String.valueOf(obj)) : getCodedValueAndFormat(featureType, obj, popupFieldInfo);
    }

    protected String getCodedValueAndFormat(FeatureType featureType, Object obj, PopupFieldInfo popupFieldInfo) {
        ArcGISPopupInfo arcGISPopupInfo;
        if (obj == null) {
            return "";
        }
        if (!(this.c.a instanceof ArcGISPopupInfo)) {
            arcGISPopupInfo = new ArcGISPopupInfo(this.c.a);
        } else {
            arcGISPopupInfo = (ArcGISPopupInfo) this.c.a;
        }
        String obj2 = obj.toString();
        Field field = arcGISPopupInfo.getField(popupFieldInfo.getFieldName());
        Domain domain = getDomain(featureType, popupFieldInfo, field);
        if (domain != null && (domain instanceof CodedValueDomain)) {
            CodedValueDomain codedValueDomain = (CodedValueDomain) domain;
            String str = codedValueDomain.getCodedValues().get(String.valueOf(obj));
            if (str == null) {
                if (field.getFieldType() == 30) {
                    Iterator<Map.Entry<String, String>> it = codedValueDomain.getCodedValues().entrySet().iterator();
                    while (true) {
                        if (!it.hasNext()) {
                            break;
                        }
                        Map.Entry<String, String> next = it.next();
                        if (!g.a(next.getKey()) && Double.valueOf(next.getKey()).equals((Double) obj)) {
                            str = next.getValue();
                            break;
                        }
                    }
                }
                return str == null ? String.valueOf(obj) : str;
            }
            return str;
        }
        boolean z = false;
        if (popupFieldInfo.getFieldName().equals(arcGISPopupInfo.getTypeIdField())) {
            FeatureType[] types = arcGISPopupInfo.getTypes();
            if (types != null) {
                int length = types.length;
                int i = 0;
                while (true) {
                    if (i >= length) {
                        break;
                    }
                    FeatureType featureType2 = types[i];
                    if (featureType2.getTemplates() == null) {
                        if (featureType2.getId().equals(String.valueOf(obj))) {
                            obj2 = featureType2.getName();
                            break;
                        }
                    } else {
                        FeatureTemplate[] templates = featureType2.getTemplates();
                        int length2 = templates.length;
                        int i2 = 0;
                        while (true) {
                            if (i2 < length2) {
                                FeatureTemplate featureTemplate = templates[i2];
                                if (featureTemplate.getPrototype() != null) {
                                    Object obj3 = featureTemplate.getPrototype().get(popupFieldInfo.getFieldName());
                                    if (obj3 == null) {
                                        obj3 = featureType2.getId();
                                    }
                                    if (obj3.equals(obj)) {
                                        obj2 = featureTemplate.getName();
                                        break;
                                    }
                                }
                                i2++;
                            }
                        }
                    }
                    i++;
                }
            }
        } else {
            try {
                if (popupFieldInfo.getFormat() != null) {
                    obj2 = popupFieldInfo.getFormat().formattedValue(obj);
                    z = true;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (!z && field != null && field.getFieldType() == 50) {
                if (obj instanceof Number) {
                    return formatValue(new Date(((Long) obj).longValue()));
                }
                try {
                    return formatValue(DateFormat.getDateInstance().format(obj));
                } catch (IllegalArgumentException unused) {
                    return obj.toString();
                }
            }
        }
        return obj2;
    }

    public Domain getDomain(FeatureType featureType, PopupFieldInfo popupFieldInfo, Field field) {
        ArcGISPopupInfo arcGISPopupInfo;
        CodedValueDomain codedValueDomain;
        if (!(this.c.a instanceof ArcGISPopupInfo)) {
            arcGISPopupInfo = new ArcGISPopupInfo(this.c.a);
        } else {
            arcGISPopupInfo = (ArcGISPopupInfo) this.c.a;
        }
        String typeIdField = arcGISPopupInfo.getTypeIdField();
        CodedValueDomain codedValueDomain2 = null;
        if (typeIdField != null && typeIdField.equals(popupFieldInfo.getFieldName())) {
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            FeatureType[] types = arcGISPopupInfo.getTypes();
            if (types != null) {
                for (FeatureType featureType2 : types) {
                    if (featureType2 != null && featureType2.getName() != null) {
                        linkedHashMap.put(featureType2.getId(), featureType2.getName());
                    }
                }
            }
            if (linkedHashMap.size() > 0) {
                codedValueDomain = new CodedValueDomain(popupFieldInfo.getFieldName(), linkedHashMap);
                if (codedValueDomain == null || featureType == null || featureType.getDomains() == null) {
                    codedValueDomain2 = codedValueDomain;
                } else {
                    Domain domain = featureType.getDomains().get(popupFieldInfo.getFieldName());
                    if (!(domain instanceof InheritedDomain)) {
                        codedValueDomain2 = domain;
                    }
                }
                return (codedValueDomain2 != null || field == null) ? codedValueDomain2 : field.getDomain();
            }
        }
        codedValueDomain = null;
        if (codedValueDomain == null) {
        }
        codedValueDomain2 = codedValueDomain;
        if (codedValueDomain2 != null) {
            return codedValueDomain2;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public String a(FeatureType featureType, String str) {
        if (str != null) {
            Matcher matcher = Pattern.compile("\\{[^\\}]*\\}").matcher(str);
            if (this.c.b == null || this.c.b.getAttributes().isEmpty()) {
                return str;
            }
            String str2 = str;
            boolean z = false;
            while (matcher.find()) {
                String substring = matcher.group().substring(1, matcher.group().indexOf("}"));
                Object attributeValue = this.c.b.getAttributeValue(substring);
                if (attributeValue != null && (this.c.a instanceof ArcGISPopupInfo)) {
                    Iterator<PopupFieldInfo> it = ((ArcGISPopupInfo) this.c.a).getFieldInfos().values().iterator();
                    while (true) {
                        if (!it.hasNext()) {
                            break;
                        }
                        PopupFieldInfo next = it.next();
                        if (next.getFieldName().equalsIgnoreCase(substring)) {
                            attributeValue = getCodedValueAndFormat(featureType, attributeValue, next);
                            break;
                        }
                    }
                }
                str2 = Pattern.compile(String.format("\\{%1$s\\}", substring)).matcher(str2).replaceAll(attributeValue == null ? "" : Matcher.quoteReplacement(attributeValue.toString()));
                z = true;
            }
            return !z ? str : str2;
        }
        return str;
    }

    public String formatValue(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Double) {
            return b.format(((Double) obj).doubleValue());
        }
        if (obj instanceof Long) {
            return b.format(((Long) obj).longValue());
        }
        if (obj instanceof Integer) {
            return b.format(Long.valueOf(((Integer) obj).intValue()).longValue());
        }
        if (obj instanceof Float) {
            return b.format(Double.valueOf(((Float) obj).floatValue()).doubleValue());
        }
        if (obj instanceof Date) {
            return a.format(obj);
        }
        return obj.toString();
    }
}
